<template>
  <view class="category-list-container">
    <!-- 顶部导航栏 -->
<!--    <view class="top-bar">
      <text class="title">商品分类</text>
      <view class="right-icon" @click="handleBack">
        <image src="/static/jiantou2_1.png" mode="aspectFit" class="back-icon"></image>
      </view>
    </view> -->

    <!-- 分类列表 -->
    <view class="category-items">
      <view class="category-item" v-for="(category, index) in categories" :key="category.id">
        <!-- 分类基本信息 -->
        <view class="category-info">
          <view class="category-header">
            <text class="category-name">{{ category.name }}</text>
            <text class="category-weight">权重: {{ category.weigh }}</text>
          </view>
          <text class="category-desc">{{ category.description || '暂无描述' }}</text>
        </view>
        
        <!-- 操作按钮 -->
        <view class="action-buttons">
          <view class="action-button edit-button" @click="editCategory(category.id)">
            <image src="/static/xiugai07_1.png" mode="aspectFit" class="action-icon"></image>
            <text class="action-text">修改</text>
          </view>
          <view class="action-button delete-button" @click="deleteCategory(category.id)">
            <image src="/static/shanchu_1.png" mode="aspectFit" class="action-icon"></image>
            <text class="action-text">删除</text>
          </view>
        </view>
      </view>
      
      <!-- 空状态提示 -->
      <view v-if="categories.length === 0" class="empty-state">
        <text class="empty-text">暂无商品分类</text>
      </view>
    </view>
    
    <!-- 添加分类按钮 -->
    <view class="add-button-container">
      <button class="add-button" @click="showAddCategoryModal">
        <image src="/static/tianjia_2.png" mode="aspectFit" class="add-icon"></image>
        <text class="add-text">添加分类</text>
      </button>
    </view>
    
    <!-- 添加/编辑分类弹窗 -->
    <uv-popup ref="popup" closeable round="10" mode="center" @change="change">
      <view class="modal-content">
        <view class="modal-header">
          <text class="modal-title">{{ isEditing ? '编辑分类' : '添加分类' }}</text>
  <!--        <text class="close-button" @click="closeModal">&times;</text> -->
        </view>
        
        <view class="modal-body">
          <view class="form-group">
            <text class="label">分类名称</text>
            <input type="text" v-model="formData.name" placeholder="请输入分类名称" class="input-field">
          </view>
          
          <view class="form-group">
            <text class="label">分类描述</text>
            <textarea v-model="formData.description" placeholder="请输入分类描述" class="textarea-field"></textarea>
          </view>
          
          <view class="form-group">
            <text class="label">权重</text>
            <input type="text" v-model="formData.weigh" placeholder="请输入权重，数字越大排序越靠前" class="input-field">
          </view>
        </view>
        
        <view class="modal-footer">
          <button class="cancel-btn" @click="closeModal">取消</button>
          <button class="confirm-btn" @click="saveCategory">{{ isEditing ? '保存修改' : '添加' }}</button>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
import uvPopup from '@/uni_modules/uv-popup/components/uv-popup/uv-popup.vue'
import store from '@/store'
import { getgoodscategoryadd,getgoodscategorydel,getgoodscategoryedit,getgoodscategoryedits,getgoodscategoryindex } from '../../api';
export default {
  components: {
    uvPopup
  },
  data() {
    return {
      categories: [
        {
          id: 1,
          name: '台球配件',
          description: '各类台球用品及配件',
          weight: 100
        },
        {
          id: 2,
          name: '台球杆',
          description: '专业台球杆系列',
          weight: 90
        },
        {
          id: 3,
          name: '运动服装',
          description: '台球运动相关服装',
          weight: 80
        }
      ],

      isEditing: false,
      editingId: null,
	  
      formData:{
        name: '',
        description: '',
        weigh: '' // 默认权重
      }
    };
  },
  onShow() {
  	this.init();
  },
  methods: {
	 async init(){
		 console.log('shopid',store.state.user)
		  let res=await getgoodscategoryindex({shopid:store.state.user.userInfo.id,page:'1',linit:10});
		  if(res.data.code){
			  console.log('222222222');
			  this.categories=res.data.data.rows;
		  }
		   console.log(res);
	  },
      // 返回上一页
      handleBack() {
        uni.navigateBack();
      },
      
      // 处理弹窗状态变化
      change(e) {
        // 可以在这里处理弹窗的显示/隐藏状态变化
        console.log('popup status changed:', e);
      },
    
    // 打开添加分类弹窗
    showAddCategoryModal() {
      this.isEditing = false;
      this.editingId = null;
      this.resetForm();
      this.$refs.popup.open();
    },
    
    // 编辑分类
   async editCategory(id) {
			let res= await getgoodscategoryedit({shopid:store.state.user.userInfo.id,ids:id});
			if(res.data.code==1){
				this.isEditing = true;
				this.editingId = id;
				this.formData = {
				  name: res.data.data.name,
				  description: res.data.data.description,
				  weigh: res.data.data.weigh
				};
				this.$refs.popup.open();
			}

    },
    
    // 删除分类
    deleteCategory(id) {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除该分类吗？',
        success: (res) => {
          if (res.confirm) {
            // 实际项目中这里应该调用API删除分类
            // 这里仅做本地删除处理
       console.log('111111111')
		this.dellist(id);
            // const index = this.categories.findIndex(item => item.id === id);
            // if (index !== -1) {
            //   this.categories.splice(index, 1);
            //   uni.showToast({
            //     title: '删除成功',
            //     icon: 'success'
            //   });
            // }
          }
        }
      });
    },
  async  dellist(id){
    		let res=await getgoodscategorydel({shopid:store.state.user.userInfo.id,ids:id});
    		if(res.data.code==1){
    		  uni.showToast({
    		   title: '删除成功',
    		   icon: 'success'
    		  });
    		  this.init();
    		}
    	},
    // 保存分类信息
   async saveCategory() {
      // 表单验证
      if (!this.formData.name.trim()) {
        uni.showToast({
          title: '请输入分类名称',
          icon: 'none'
        });
        return;
      }
      
      if (!this.formData.weigh || this.formData.weigh < 0) {
        uni.showToast({
          title: '请输入有效的权重值',
          icon: 'none'
        });
        return;
      }
      
      if (this.isEditing) {
        // 编辑现有分类
        // const index = this.categories.findIndex(item => item.id === this.editingId);
            
			let res= await getgoodscategoryedits({shopid:store.state.user.userInfo.id,ids:this.editingId,...this.formData});
			if(res.data.code==1){
				uni.showToast({
				  title: '更新成功',
				  icon: 'success'
				});
				this.$refs.popup.close();
				this.init();
			}
          // 实际项目中这里应该调用API更新分类
          // 这里仅做本地更新处理
          // this.categories[index] = { ...this.formData, id: this.editingId };
          // 按权重重新排序
          // this.sortCategoriesByWeight();

        
      } else {
        // 添加新分类
        // 实际项目中这里应该调用API添加分类
        // 这里仅做本地添加处理
        // const newCategory = {
        //   ...this.formData,
        //   id: Date.now() // 使用时间戳作为临时ID
        // };
		let res= await getgoodscategoryadd({shopid:store.state.user.userInfo.id,...this.formData})
        if(res.data.code){
			uni.showToast({
			  title: '添加成功',
			  icon: 'success'
			});
			    this.$refs.popup.close();
			this.init();
		}
		
		this.categories.unshift(newCategory);
		
        // 按权重重新排序
        this.sortCategoriesByWeight();
      }
      
      this.closeModal();
    },
    
    // 按权重排序分类
    sortCategoriesByWeight() {
      this.categories.sort((a, b) => b.weight - a.weight);
    },
    
    // 重置表单
    resetForm() {
      this.formData = {
        name: '',
        description: '',
        weight: 50
      };
    },
    
    // 关闭弹窗
    closeModal() {
      this.$refs.popup.close();
      // 延迟重置表单，避免动画期间看到重置过程
      setTimeout(() => {
        this.resetForm();
      }, 300);
    }
  },
  onLoad() {
    // 页面加载时按权重排序分类
    this.sortCategoriesByWeight();
  }
};
</script>

<style scoped>
.category-list-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-bottom: 30rpx;
}

/* 顶部导航栏 */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 40rpx 30rpx 30rpx;
  background-color: #fff;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  position: relative;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.back-icon {
  width: 40rpx;
  height: 40rpx;
}

/* 添加按钮 */
.add-button-container {
  padding: 30rpx;
}

.add-button {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #4cd964;
  color: #fff;
  border-radius: 20rpx;
  padding: 20rpx;
  font-size: 28rpx;
  border: none;
  width: 100%;
}

.add-icon {
  width: 30rpx;
  height: 30rpx;
  margin-right: 10rpx;
}

/* 分类列表 */
.category-items {
  padding: 0 30rpx;
}

.category-item {
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.category-info {
  margin-bottom: 20rpx;
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10rpx;
}

.category-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.category-weight {
  font-size: 26rpx;
  color: #666;
  background-color: #f2f2f2;
  padding: 4rpx 16rpx;
  border-radius: 10rpx;
}

.category-desc {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  justify-content: flex-end;
  border-top: 1rpx solid #eee;
  padding-top: 20rpx;
}

.action-button {
  display: flex;
  align-items: center;
  margin-left: 40rpx;
}

.action-icon {
  width: 28rpx;
  height: 28rpx;
  margin-right: 8rpx;
}

.edit-button .action-text {
  color: #007aff;
}

.delete-button .action-text {
  color: #ff3b30;
}

.action-text {
  font-size: 28rpx;
}

/* 空状态 */
.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 100rpx 0;
}

.empty-text {
  font-size: 32rpx;
  color: #999;
}

/* 弹窗样式 */
.modal-content {
  background-color: #fff;
  border-radius: 30rpx 30rpx 0 0;
  padding-bottom: 40rpx;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.close-button {
  font-size: 40rpx;
  color: #999;
}

.modal-body {
  padding: 30rpx;
}

.form-group {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 15rpx;
}

.input-field {
  width: 100%;
/*  padding: 20rpx; */
  border: 1rpx solid #ddd;
  border-radius: 10rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.textarea-field {
  width: 100%;
  padding: 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 10rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  min-height: 150rpx;
  resize: none;
}

.modal-footer {
  display: flex;
  padding: 0 30rpx;
}

.cancel-btn {
  flex: 1;
  margin-right: 20rpx;
  background-color: #f2f2f2;
  color: #666;
  border: none;
  border-radius: 10rpx;
  padding: 20rpx;
}

.confirm-btn {
  flex: 1;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 10rpx;
  padding: 20rpx;
}
</style>